package com.hl.mapper;


import com.hl.pojo.CourseReview;
import org.apache.ibatis.annotations.*;

import java.util.List;
//课程评价数据访问层
@Mapper
public interface CourseReviewMapper {

    // 插入课程评价
    @Insert("INSERT INTO course_review (user_id, context, time, score,course_id) VALUES (#{userId},#{context}, #{time}, #{score},#{courseId})")
    Integer addCourseReview(CourseReview courseReview);

    // 根据id删除课程评价（逻辑删除）
    @Update("UPDATE course_review SET del = 1 WHERE id = #{id}")
    Integer deleteCourseReviewById(Integer id);

    // 根据id查询课程评价
    @Select("SELECT * FROM course_review WHERE id = #{id} AND del = 0")
    CourseReview findCourseReviewById(Integer id);

    // 查询所有未删除的课程评价
    @Select("SELECT c.id,c.user_id,c.course_id,c.context,c.time,c.score,c.del  FROM course_review c  WHERE  del = 0")
    @Results({
            @Result(property = "username",column = "user_id" ,
                    one = @One(select = "a1"))
    })
    List<CourseReview> findAllCourseReview();
    @Select("select username from system_user where id = #{id}")
    public String a1(Integer id);

    // 根据课程id查询该课程的所有未删除评价
    @Select("SELECT c.id,c.user_id,c.course_id,c.context,c.time,c.score,c.del  FROM course_review c  WHERE c.course_id=#{id} and del = 0")
    @Results({
            @Result(property = "username",column = "user_id" ,
                    one = @One(select = "a1"))
    })

    List<CourseReview> findCourseReviewByCourseId(Integer courseId);

    // 更新课程评价
    @Update("UPDATE course_review SET user_id=#{userId}, course_id=#{courseId}, content=#{context}, time=#{time}, score=#{score} WHERE id=#{id}")
    Integer updateCourseReview(CourseReview courseReview);
}
